﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using BusinessFacade;
using System.Data;
using Comm;
using System.Web;
using MedicareNoteManagerWeb.ServiceReference1;
namespace MedicareNoteManagerWeb
{
    public partial class MessageTemplatesAudit : BasePage
    {
        mta_MessagePlanAudit_Model model = new mta_MessagePlanAudit_Model();
        Busi_MessagePlanAudit bmpa = new Busi_MessagePlanAudit();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillDropDownList("MessagePlanDDL", "MoubleName", "MoubleName", "请选择模板", this.ddl_PlanState);
            }
            Grd_User.DataSource = bmpa.MessagePlanAll();
            Grd_User.DataBind();
        }

        protected void btn_Select_Click(object sender, EventArgs e)
        {
            DataBind();
        }
        public void DataBind() 
        {
            model.InputPlanName = this.tb_PlanName.Text.Trim();
            model.PalnTemplate = this.ddl_PlanState.SelectedValue;
            if (model.InputPlanName == "" && model.PalnTemplate == "0")
            {
                Grd_User.DataSource = bmpa.MessagePlanAll();
                Grd_User.DataBind();
            }
            else if (model.InputPlanName != "" || model.PalnTemplate == "0")
            {
                Grd_User.DataSource = bmpa.MessagePlanName(model);
                Grd_User.DataBind();
            }
            else if (model.InputPlanName == "" || model.PalnTemplate != "0")
            {
                Grd_User.DataSource = bmpa.MessagePlanTemple(model);
                Grd_User.DataBind();
            }
            else
            {
                Grd_User.DataSource = bmpa.MessagePlanTwo(model);
                Grd_User.DataBind();
            }
        }

        protected void Grd_User_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "read")
            {
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                model.RowIndex = gvrow.RowIndex; //获取到行索引
                Label lbl_PlanID = Grd_User.Rows[model.RowIndex].FindControl("lbl_PlanID") as Label;
                model.PlanID = int.Parse(lbl_PlanID.Text);
                //DataTable dt = bmpa.ReadMessage(model);
                //foreach (DataRow dr in dt.Rows)
                //{
                //    model.PlanName = dr["planname"].ToString();
                //    model.PlanMouble = dr["planmouble"].ToString();
                //    model.PlanMain = dr["notecontent"].ToString();
                //    model.SendTime = Convert.ToDateTime(dr["sendtime"].ToString());
                //}
                //Response.Redirect("ReadMessagePlan.aspx?planID="+model.PlanID+"");
            }
            if (e.CommandName == "pass")
            {
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                model.RowIndex = gvrow.RowIndex; //获取到行索引
                Label lbl_PlanID = Grd_User.Rows[model.RowIndex].FindControl("lbl_PlanID") as Label;
                model.PlanID = int.Parse(lbl_PlanID.Text);
                Label lb_Pass = Grd_User.Rows[model.RowIndex].FindControl("lb_Pass") as Label;
                //lb_Pass.Attributes.Add("onclick", "javascript:return confirm('确定通过吗？');");
                int result = bmpa.PassPlan(model);
                if (result > 0)
                {
                    MessageBox.ShowDialog(this.Page, "OK", "success");
                    WSAddPlan ws = new WSAddPlan();
                    Busi_MessagePlanAudit bm = new Busi_MessagePlanAudit();
                    DataTable dt =bm.ReadMessage(model);
                    ws.CreateTestTask(DateTime.Parse(dt.Rows[0]["sendtime"].ToString()), dt.Rows[0]["planname"].ToString());
                    Page_Load(sender, e);
                }
            }
            if (e.CommandName == "return")
            {
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                model.RowIndex = gvrow.RowIndex; //获取到行索引
                Label lbl_PlanID = Grd_User.Rows[model.RowIndex].FindControl("lbl_PlanID") as Label;
                model.PlanID = int.Parse(lbl_PlanID.Text);
                Label lb_Pass = Grd_User.Rows[model.RowIndex].FindControl("lb_Return") as Label;
                //lb_Pass.Attributes.Add("onclick", "javascript:return confirm('确定不通过吗？');");
                int result = bmpa.ReturnPlan(model);
                if (result > 0)
                {
                    MessageBox.ShowDialog(this.Page, "OK", "success");
                }
            }
        }

        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            DataBind();
        }
    }
}